1853C - Ntarsis' Set - CodeForces Solution


binary search math

Please click on ads to support us..

Python Code:

t=int(input())
for _ in range(t):
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    if a[0]!=1 or k==0:
        print(1)
        continue
    
    c=1
    d=0
    jmp=0    while jmp!=k:
        for i in range(d,n+1):
            if a[i-1]<c+i:
                if i==n:
                    c+=i
                    d=i
                    jmp+=1
                    break
                elif c+i<a[i]:
                    c+=i
                    d=i
                    jmp+=1
                    break
    
    print(c)

C++ Code:

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "templates/temp.h"
#endif
using namespace std;
using ll=long long;

void solve(){
    int n,k;cin>>n>>k;
    vector<ll>a(n);
    for(auto &x:a){
        cin>>x;
    }
    ll ans=0,l=1,r=1e12;
    while(l<=r){
        ll mid=(l+r)>>1;
        ll cur=mid;
        for(int i=0;i<k;i++){
            cur-=upper_bound(begin(a),end(a),cur)-begin(a);
        }
        if(cur>0){
            ans=mid;
            r=mid-1;
        }
        else{
            l=mid+1;
        }
    }
    cout << ans << endl;
}
int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int t=1;
    cin>>t;
    while(t-->0){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND
363C - Fixing Typos
1401A - Distance and Axis
658A - Bear and Reverse Radewoosh
1721E - Prefix Function Queries
977E - Cyclic Components
1140D - Minimum Triangulation
75C - Modified GCD
1722A - Spell Check
1722B - Colourblindness
1722D - Line
1722C - Word Game
1722G - Even-Odd XOR
552E - Vanya and Brackets
933A - A Twisty Movement
1722F - L-shapes
1196B - Odd Sum Segments
1325D - Ehab the Xorcist
552B - Vanya and Books
1722E - Counting Rectangles
168A - Wizards and Demonstration
168B - Wizards and Minimal Spell
7A - Kalevitch and Chess
912B - New Year's Eve
1537C - Challenging Cliffs
879B - Table Tennis
1674E - Breaking the Wall
1282A - Temporarily unavailable